﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Data;

namespace MyImage
{
    public partial class _Default : System.Web.UI.Page
    {

        protected System.Web.UI.WebControls.Image imgImage;
        private int THUMBNAIL_SIZE = 45;

        protected void Page_Load(object sender, EventArgs e)
        {


        }
        protected byte[] Bitmap2Byte(Bitmap bitmap)
        {
            try
            {
                MemoryStream memoryStream = new MemoryStream();
                byte[] byteResult = null;
                bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byteResult = memoryStream.GetBuffer();

                bitmap.Dispose();
                if (memoryStream != null)
                    memoryStream.Close();

                return byteResult;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        protected void ShowImage(int id)
        {
            ParseImage.THUMBNAIL_SIZE = 500;
            ParseImage.USE_SIZE_FOR_HEIGHT = false ;

            imgImage = new System.Web.UI.WebControls.Image();
            //// set the source to the page that generates the thumbnail image
            imgImage.ImageUrl = ParseImage.PAGE_NAME + "?" +
               ParseImage.IMAGE_ID + "=" + id;

            previewImage.Controls.Clear();
            previewImage.Controls.Add(imgImage);


        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            // get name file and extension of file
            string pathFile = FileUpload1.PostedFile.ContentType;

            if (pathFile.ToLower().StartsWith("image"))
            {
                Bitmap bitmapImage = (Bitmap)System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
                int id = 0;

                int RowsAff = DataAcess.SaveImage(ref id, Bitmap2Byte(bitmapImage),false);
                if (RowsAff > 0)
                {
                    Button2_Click(sender, e);
                    ShowImage(id);
                    
                }
                else
                {
                    lblMessage.Text = "loi upload";
                }
            }
            else
            {
                lblMessage.Text = "error, file not image";

            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet listImage = DataAcess.GetAllImage(false);
                dlImages.DataSource = listImage;
                dlImages.DataBind();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void dlImages_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            System.Web.UI.WebControls.Image img = null;

            // make sure this is an item in the data list (not header etc.)
            if ((e.Item.ItemType == ListItemType.Item) ||
                (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                // get a reference to the image used for the bar in the row
                img = (System.Web.UI.WebControls.Image)
                    (e.Item.FindControl("Image1"));

                //get the img_pk from DataRow being bound
                int img_pk = Convert.ToInt16(
                    ((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString());
                
                ParseImage.THUMBNAIL_SIZE = this.THUMBNAIL_SIZE;
                ParseImage.USE_SIZE_FOR_HEIGHT = false;
                // set the source to the page that generates the thumbnail image
                img.ImageUrl = ParseImage.PAGE_NAME + "?" + ParseImage.IMAGE_ID + "=" + img_pk;
            }
        }

        protected void dlImages_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
